﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SQLite.NET;
using MPMusicVids.Data;
using MPMusicVids.Log;
using MPMusicVids.Importer;
using System.IO;
using System.Threading;

namespace MPMusicVids
{
    public partial class Parsing : UserControl
    {
        DataManager dm = MusicVidsMain.dm;
        Stack parsedStack = new Stack();
        private Logging log = Logging.getLogger();
        public string extensions;
        delegate void SetDGVCallback(string shortFilename, string Artist, string Title, string longFilename);
        delegate void ClearDGVCallback();

        public Parsing()
        {
            InitializeComponent();
        }

        public void doParseTest()
        {
            extensions = dm.getExtensions();
            bool errors = false;
            ClearDGV();
            log.Append("Running parsing test");
            //Something to do here to make it do the parsing     
            parsedStack = Scan.getStack();
            do
            {
                string[] tmp = new string[4];
                try
                {
                    tmp = (string[])parsedStack.Pop();
                }
                catch (Exception e)
                { break; }
                SetDGV(tmp[3], tmp[0], tmp[1], tmp[2]);
                if (tmp[0] == "ERROR")
                    errors = true;
            } while (true);
            if (errors)
                MessageBox.Show("Some files did not match the parsing expression\nPlease check through your results for any\nmarked error and rename by double clicking on filename.");
        }

        private void dataGridView3_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //Get which filename was clicked, then open dialog to rename.
            int row = e.RowIndex;
            int col = e.ColumnIndex;
            if (col == 0)
            {
                //Open rename dialog box
                dlgRename rename = new dlgRename(dgvParse.Rows[row].Cells[0].Value.ToString(), dgvParse.Rows[row].Cells[3].Value.ToString());
                if (rename.ShowDialog() == DialogResult.OK)
                {

                }
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            mainConfig mc = (mainConfig)this.Parent.Parent.Parent;
            mc.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Rescan rs = new Rescan();
            if (rs.ShowDialog() == DialogResult.OK)
            {
                TabControl tc1 = (TabControl)this.Parent.Parent;
                tc1.SelectedIndex = 0;
            }
        }

        private void SetDGV(string shortFilename, string Artist, string Title, string longFilename)
        {
            if (this.dgvParse.InvokeRequired)
            {
                SetDGVCallback d = new SetDGVCallback(SetDGV);
                this.Invoke(d, new object[] { shortFilename, Artist, Title, longFilename });
            }
            else
            {
                int i = dgvParse.Rows.Add(shortFilename, Artist, Title, longFilename);
                if (Artist == "ERROR" || Title == "ERROR")
                    dgvParse.Rows[i].DefaultCellStyle.BackColor = Color.Red;
            }
        }

        private void ClearDGV()
        {
            if (this.dgvParse.InvokeRequired)
            {
                ClearDGVCallback d = new ClearDGVCallback(ClearDGV);
                this.Invoke(d, new object[] { });
            }
            else this.dgvParse.Rows.Clear();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(doParseTest)).Start();
        }

    }
}
